package com.explorati.LeetCode206.reverseLinkedList;

/**
 * 206. Reverse Linked List 第一种解法：递归
 * 
 * Input: 1->2->3->4->5->NULL Output: 5->4->3->2->1->NULL
 * 
 * @author explorati
 *
 */
public class Solution {
	public class ListNode {
		int val;
		ListNode next;

		ListNode(int x) {
			val = x;
		}
	}

	public ListNode reverseList(ListNode head) {
		if (head == null) {
			return null;
		}
		if (head.next == null) {
			return head;
		}
		ListNode reverse_node_head = reverseList(head.next);
		ListNode cur = reverse_node_head;
		while (cur.next != null) {
			cur = cur.next;
		}
		cur.next = head;
		head.next = null;
		return reverse_node_head;
	}
}
